<?php

namespace app\common\model;

use think\Db;
use think\Model;

class Taobao extends Model
{

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    public static $modelCoupon = 'a_taobao_coupon';
    public static $modelConfig = 'mall_admin';
    public static $modelCategory = 'a_taobao_func';
    public static $modelAd = 'a_ad';
    

    public static function getTabaoCouponList($map ,   $order = [] , $page =1 , $pageSize =20){
        $result = Db::table(self::$modelCoupon)->where($map);
        if(isset($map['q']) && $map['q']){
            $result = $result->whereLike('title', '%'.$map['q'].'%');
        }
        
        $result = $result->page($page , $pageSize);
        if($order){
            foreach($order as $k=>$v){
                $result = $result->order($k , $v);
            }
        }
        $result = $result->select();
        if($result)
        {
            return $result;
        }else{
            
            return array();
        }
    }
    public static function addTabaoCoupon($data){
        $result = Db::table(self::$modelCoupon)->insert($data,false,true);
        if($result)
        {
            return $result ;
        }else{
            
            return false;
        }
    }
    public static function getTabaoCoupon($map){
        $result = Db::table(self::$modelCoupon)->where($map)->find();
        if($result)
        {
            return $result ;
        }else{
            
            return array();
        }
    }
    public static function getTabaoCouponCount($map){
        return Db::table(self::$modelCoupon)->where($map)->count(1);
    }
    public static function getTabaoCategoryList($map ,   $order = [] ){
        $result = Db::table(self::$modelCategory)->where($map);
        if($order){
            foreach($order as $k=>$v){
                $result = $result->order($k , $v);
            }
        }
        $result = $result->select();
        if($result)
        {
            return $result;
        }else{
            
            return array();
        }
    }
    public static function getTabaoCategory($map ,   $column = '' ){
        $result = Db::table(self::$modelCategory)->where($map);
        return $column ? $result->value($column) : $result->find();
    }
    public static function getTabaoAdList($map,$order = []){
        $result = Db::table(self::$modelAd)->where($map);
        if($order){
            foreach($order as $k=>$v){
                $result = $result->order($k , $v);
            }
        }
        $result = $result->select();
        if($result)
        {
            return $result;
        }else{
            
            return array();
        }
    }
    public static function getTaobaoConfig($map = ['uid'=>101]){
        return Db::table(self::$modelConfig)->where($map)->find();
    }
    public static function clearCache(){
        return Db::table(self::$modelCoupon)->execute("TRUNCATE `a_taobao_coupon`");
    }

}
